package com.gitee.wsl.net.server

import com.gitee.wsl.transform.validator.Validator
import com.gitee.wsl.transform.validator.validateWithResult
import io.ktor.server.plugins.requestvalidation.RequestValidationConfig
import io.ktor.server.plugins.requestvalidation.ValidationResult
import io.ktor.server.request.*


/**
 * Registers a new [validator], which will be executed for each [received][receive] request body.
 */
inline fun <reified T : Any,V:Validator<T,V>> RequestValidationConfig.registerValidator(validator: Validator<T, V>) {
    validate<T> {
        val ret = validator.validateWithResult(it)
        if(ret.isSuccess)
           ValidationResult.Valid
        else
            ValidationResult.Invalid(ret.exceptionOrNull()?.message?:"not validator with : $it")
    }
}